var util = require('../../libs/util.js');
var app = getApp();
Page({
  data: {
    step:1,
    img_driving_licence: "/images/drivinglicense.jpg",
    name: "",
    id_card: "",
    license_number:"",
    modalName: '',
    progress: 0,
    totalBytesSent: 0,
    totalBytesExpectedToSend: 0,
    workingBtn:false,
    form:'add'
  },
  onShareAppMessage: function () {
    return {
      title: app.appName,
      path: '/pages/index/index'
    }
  },
  onLoad: function (e) {
    wx.showToast({
      title: "页面加载中\n请稍候...",
      icon: "loading",
      duration: 5000
    });
    this.setData({
      nofuser:app.globalData.user_id == 1?false:true,
      truename:app.globalData.truename,
      server:app.server
    })
    if(e.form){
      this.setData({
        form:e.form
      })
    }
  },
  onReady: function () {
    wx.hideToast()
  },
  bindInput: function (e) {
    var item = String(e.currentTarget.id), item_value = {};
    item_value[item] = e.detail.value;
    this.setData(item_value);
  },
  chooseImage: function (e) {
    var that = this;
    wx.chooseImage({
      count:1,
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // wx.showLoading({
        //   title: '上传中...',
        //   mask: true
        // })

        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var item = String(e.currentTarget.id),item_value = {};
        item_value[item] = res.tempFilePaths[0];
        that.setData(item_value);
        that.setData({
          modalName: 'uploadModal',
          progress: 0,
          totalBytesSent: 0,
          totalBytesExpectedToSend: 0
        })
        that.uploadTask = wx.uploadFile({
          url: app.server + '/wechat/passport/uploadCard_drivingLicence',
          header:{
            'Authorization': app.globalData.token
          },
          filePath: res.tempFilePaths[0],
          name: 'file',
          formData: {
            'CO_ID': app.globalData.CO_ID,
            'user_id': app.globalData.user_id
          },
          success: function (res) {
            var data = JSON.parse(res.data);
            console.log(data)
            if (data.success){
              that.setData({
                'license_number': data.license_number,
                'id_card': data.license_number,
                "img_driving_licence": data.data,
                "driving_date": data.issue_date,
                "driving_model": data.class,
                "nationality": data.nationality,
                "driving_valid_date": data.valid_date
              });
              if(data.name){
                that.setData({
                  "name": data.name
                });
              }
              // var upload_item = String(e.currentTarget.id), item_value = {};
              // var path = data.data[0].path;
              // if(path.indexOf("http") == -1){
              //   path = app.server + path
              // }
              // item_value[upload_item] = path;
              // that.setData(item_value);
            }else{
              wx.showModal({
                content: data.data,
                confirmText: "确定",
                showCancel: false
              })
            }
          },
          complete: function () {
            that.setData({
              modalName: ''
            })
          }
        })
        that.uploadTask.onProgressUpdate((res) => {
          that.setData({
            progress: res.progress,
            totalBytesSent: Number(res.totalBytesSent / 1000 / 1000).toFixed(3),
            totalBytesExpectedToSend: Number(res.totalBytesExpectedToSend / 1000 / 1000).toFixed(3)
          })
        })
      }
    })
  },
  cannelUpload(e) {
    this.setData({
      modalName: null
    })
    this.uploadTask.abort()
  },
  previewImage: function (e) {
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: this.data.files // 需要预览的图片http链接列表
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  submitForms: function (e) {
    var that = this;
    this.setData({
      workingBtn:true
    })
    wx.showLoading({
      title: '请稍候...',
    });
    // if (!that.data.license_number) {
    //   wx.showToast({
    //     title: '请上传驾驶证',
    //     image: '/images/wrong.png',
    //     duration: 3000
    //   });
    //   this.setData({
    //     workingBtn:false
    //   })
    //   return false;
    // }
    if (!(that.data.name||that.data.truename)) {
      wx.showToast({
        title: '请输入姓名',
        image: '/images/wrong.png',
        duration: 3000
      });
      this.setData({
        workingBtn:false
      })
      return false;
    }
    if (!that.data.id_card) {
      wx.showToast({
        title: '请输入身份证号码',
        image: '/images/wrong.png',
        duration: 3000
      });
      this.setData({
        workingBtn:false
      })
      return false;
    }
    if (!that.data.emergency_contact) {
      wx.showToast({
        title: '请输入紧急联系人',
        image: '/images/wrong.png',
        duration: 3000
      });
      this.setData({
        workingBtn:false
      })
      return false;
    }
    if (!that.data.emergency_contact_phone) {
      wx.showToast({
        title: '请输入联系号码',
        image: '/images/wrong.png',
        duration: 3000
      });
      this.setData({
        workingBtn:false
      })
      return false;
    }
    if (!that.data.real_address) {
      wx.showToast({
        title: '请输入现住址',
        image: '/images/wrong.png',
        duration: 3000
      });
      this.setData({
        workingBtn:false
      })
      return false;
    }
    
    util.postData("/wechat/passport/regDriver", {
      CO_ID: app.globalData.CO_ID,
      openid:app.globalData.openid,
      user_id: app.globalData.user_id,
      phone: app.globalData.mobile,
      name: that.data.name,
      id_card: that.data.id_card,
      driving_date: that.data.driving_date,
      driving_model: that.data.driving_model,
      nationality: that.data.nationality,
      driving_valid_date: that.data.driving_valid_date,
      real_address: that.data.real_address,
      emergency_contact: that.data.emergency_contact,
      emergency_contact_phone: that.data.emergency_contact_phone,
      id:app.globalData.driverId,
      form:app.globalData.driverId?'edit':'add'
    }, function (res, error) {
      console.log(res); console.log(error)
      wx.hideLoading();
      if (error) {
        wx.showToast({
          title: res.data.data,
          image: '/images/wrong.png',
          duration: 3000
        });
        that.setData({
          workingBtn:false
        })
        return false;
      }
      if (res.data.success) {
        wx.showModal({
          title: '温馨提示',
          content: '恭喜你，注册司机成功！',
          success: function (res) {
            wx.redirectTo({
              url: '/pages/index/index'
            })
          }
        })
      } else{
        that.setData({
          workingBtn:false
        })
        wx.showToast({
          title: res.data.data,
          image: '/images/wrong.png',
          duration: 3000,
          mask: true
        });
      }
    });
  }
});